package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.AdminService;
import com.tgy.page.PageQuery;
import com.tgy.pojo.SysAdmin;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 21:01
 */
@Validated
@Api(value = "后台管理用户控制器", tags = {"后台管理用户管理"})
@RestController
@RequestMapping("/admin/user")
public class AdminController {

    @Resource
    private AdminService adminService;

    @ApiOperation("获取后台管理用户列表")
    @SaCheckPermission("sysadmin:query")
    @GetMapping("getAdminList")
    public LayuiTableResult getAdminList(SysAdmin bo,PageQuery pageQuery){
        return adminService.getAdminList(bo,pageQuery);
    }

    @ApiOperation("修改后台管理用户状态")
    @SaCheckPermission("sysadmin:update")
    @PostMapping("updateStatus")
    public GraceJSONResult updateStatus(SysAdmin sysAdmin){
        return adminService.updateStatus(sysAdmin.getId(),sysAdmin.getStatus());
    }

    @ApiOperation("添加后台管理用户")
    @SaCheckPermission("sysadmin:add")
    @PostMapping("addSysAdmin")
    public GraceJSONResult addSysAdmin(SysAdmin sysAdmin){
        return adminService.addSysAdmin(sysAdmin);
    }

    @ApiOperation("修改后台管理用户")
    @SaCheckPermission("sysadmin:update")
    @PostMapping("updateSysAdmin")
    public GraceJSONResult updateSysAdmin(SysAdmin sysAdmin){
        return adminService.updateSysAdmin(sysAdmin);
    }

    @ApiOperation("删除后台管理用户")
    @SaCheckPermission("sysadmin:del")
    @PostMapping("delSysAdmin")
    // @PostMapping("delSysAdmin/{id}")
    public GraceJSONResult delSysAdmin(@ApiParam("ID必须传入") String id){
        return adminService.delSysAdmin(id);
    }

    @ApiOperation("批量删除后台管理用户")
    @SaCheckPermission("sysadmin:del")
    @PostMapping("delSysAdminIds")
    public GraceJSONResult delSysAdminIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return adminService.delSysAdminIds(list);
    }

    @ApiOperation("根据ID查询后台管理用户")
    @SaCheckPermission
    @PostMapping("getSysAdminById")
    public GraceJSONResult getSysAdminById(@ApiParam("ID必须传入") String id){
        return adminService.getSysAdminById(id);
    }

    @ApiOperation("获取后台管理角色列表")
    @SaCheckPermission("sysrole:query")
    @GetMapping("getSysRoleList")
    public LayuiTableResult getSysRoleList(@ApiParam("ID必须传入") String userId,PageQuery pageQuery){
        return adminService.getAllSysRoleList(userId,pageQuery);
    }

    @ApiOperation("后台管理用户添加角色")
    @SaCheckPermission("sysadmin:power")
    @PostMapping("addRole")
    public GraceJSONResult addRole(@ApiParam("用户ID必须传入") String aid,@ApiParam("角色ID集合必须传入") String rids){
        return adminService.addRole(aid,rids);
    }

}
